import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home'
import IndexView from '../views/Index'
import GoodsManage from '../views/GoodsManage/GoodsManage'
import GoodsAdd from  '../views/GoodsAdd/GoodsAdd'
import GoodsOutStock from '../views/GoodsOutStock/GoodsOutStock'
import SalesList from  '../views/SalesList/SalesList'
import SaleStatistics from '../views/SaleStatistics/SaleStatistics'
import Login from "../views/Login/Login"

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'login',
        component: Login
    },
    {
        path: '/index',
        name: 'index',
        component: IndexView,
        children:[
            {
            path: '',
            name: 'home',
            component: Home,
                meta: {
                    title: '主页面',
                    requireAuth: true
                }
        },
            {
                path: 'goodsManage',
                name: 'goodsManage',
                component: GoodsManage,
                meta: {
                    title: '商品管理',
                    requireAuth: true
                }
            },
            {
                path: 'goodsAdd',
                name: 'goodsAdd',
                component: GoodsAdd,
                meta: {
                    title: '添加商品',
                    requireAuth: true
                }
            },
            {
                path: 'goodsOutStock',
                name: 'goodsOutStock',
                component: GoodsOutStock,
                meta: {
                    title: '出库',
                    requireAuth: true
                }
            },
            {
                path: 'salesList',
                name: 'salesList',
                component: SalesList,
                meta: {
                    title: '查询订单',
                    requireAuth: true
                }
            },
            {
                path: 'saleStatistics',
                name: 'saleStatistics',
                component: SaleStatistics
            }

        ]
    }
    // {
    //     path: '/about',
    //     name: 'about',
    //     // route level code-splitting
    //     // this generates a separate chunk (about.[hash].js) for this route
    //     // which is lazy-loaded when the route is visited.
    //     component: function () {
    //         return import(/* webpackChunkName: "about" */ '../views/Index.vue')
    //     }
    // }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    document.title = to.matched[0].meta.title; // 显示标题
    // next()
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (sessionStorage.getItem('isLogin') === 'true') {
            next()
        } else {
            // 未登录，跳转到登录页面
            next('/')
        }
    } else {
        next()
    }
})

export default router
